#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "delay.h"
#include "air32f10x.h"
#include "lcd_init.h"
#include "lcd.h"
#include "pic.h"
#include "key.h"

USART_TypeDef *USART_TEST = USART1;

void UART_Configuration(uint32_t bound);
void GPIO_Configuration(void);
void RCC_ClkConfiguration(void);
/********************************************************************************/
// GPIO输出测试，开发板自带的3个灯依次亮起并熄灭实现跑马灯效果，日志通过串口1发送，波特率为115200
/********************************************************************************/
int main(void)
{
	RCC_ClocksTypeDef clocks;
	RCC_ClkConfiguration();
	Delay_Init();				//延时初始化
	UART_Configuration(115200); //默认为串口1，波特率115200
	RCC_GetClocksFreq(&clocks); //获取系统时钟频率

	Key_Config();
	LCD_Init();
	LCD_Fill(0,0,LCD_W,LCD_H,WHITE);
	u8 menuPage = 0;
	
	while (1)
	{
		if(key_scan() == 1)
		{
			LCD_Fill(0,0,LCD_W,LCD_H,WHITE);
			menuPage++;
			if(menuPage == 3) menuPage = 0;
		}
		if(menuPage == 0)
		{
			LCD_ShowString(10,0,"Freq:",RED,WHITE,16,0);
			LCD_ShowIntNum(50,0,(clocks.SYSCLK_Frequency / 1000000),3,RED,WHITE,16);
			LCD_ShowString(10,20,"LCD_W:",RED,WHITE,16,0);
			LCD_ShowIntNum(58,20,LCD_W,3,RED,WHITE,16);
			LCD_ShowString(10,40,"LCD_H:",RED,WHITE,16,0);
			LCD_ShowIntNum(58,40,LCD_H,3,RED,WHITE,16);
			LCD_ShowPicture(100,20,40,40,gImage_1);
		}
		else if(menuPage == 1)
		{
			LCD_ShowPicture(40,0,80,80,gImage_2);
		}
		else if(menuPage == 2)
		{
			LCD_ShowPicture(5,0,150,80,gImage_3);
		}
	}
}

void RCC_ClkConfiguration(void)
{
	RCC_DeInit(); //复位RCC寄存器

	RCC_HSEConfig(RCC_HSE_ON); //使能HSE
	while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET)
		; //等待HSE就绪

	RCC_PLLCmd(DISABLE);										 //关闭PLL
	AIR_RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_27, FLASH_Div_2); //配置PLL,8*27=216MHz

	RCC_PLLCmd(ENABLE); //使能PLL
	while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
		; //等待PLL就绪

	RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //选择PLL作为系统时钟

	RCC_HCLKConfig(RCC_SYSCLK_Div1); //配置AHB时钟
	RCC_PCLK1Config(RCC_HCLK_Div2);	 //配置APB1时钟
	RCC_PCLK2Config(RCC_HCLK_Div1);	 //配置APB2时钟

	RCC_LSICmd(ENABLE); //使能内部低速时钟
	while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
		;				//等待LSI就绪
	RCC_HSICmd(ENABLE); //使能内部高速时钟
	while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET)
		; //等待HSI就绪
}

void UART_Configuration(uint32_t bound)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	USART_InitStructure.USART_BaudRate = bound;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

	USART_Init(USART_TEST, &USART_InitStructure);
	USART_Cmd(USART_TEST, ENABLE);
}

int SER_PutChar(int ch)
{
	while (!USART_GetFlagStatus(USART_TEST, USART_FLAG_TC))
		;
	USART_SendData(USART_TEST, (uint8_t)ch);

	return ch;
}

int fputc(int c, FILE *f)
{
	/* Place your implementation of fputc here */
	/* e.g. write a character to the USART */
	if (c == '\n')
	{
		SER_PutChar('\r');
	}
	return (SER_PutChar(c));
}
